package com.ruoyi.web.controller.fs;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fs.domain.FsBill;
import com.ruoyi.fs.service.IFsBillService;
import org.apache.poi.hpsf.Decimal;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsRepay;
import com.ruoyi.fs.service.IFsRepayService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 还款明细Controller
 *
 * @author hero
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/fs/repay")
public class FsRepayController extends BaseController {
    @Autowired
    private IFsRepayService fsRepayService;
    @Autowired
    private IFsBillService fsBillService;

    /**
     * 查询还款明细列表
     */
    @PreAuthorize("@ss.hasPermi('fs:repay:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsRepay fsRepay) {
        startPage();
        List<FsRepay> list = fsRepayService.selectFsRepayList(fsRepay);
        for (int i = 0; i < list.size(); i++) {
            FsRepay repay = list.get(i);
            FsBill bill = fsBillService.selectFsBillById(repay.getBillId());
            repay.setBill(bill);
        }
        return getDataTable(list);
    }

    /**
     * 导出还款明细列表
     */
    @PreAuthorize("@ss.hasPermi('fs:repay:export')")
    @Log(title = "还款明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsRepay fsRepay) {
        List<FsRepay> list = fsRepayService.selectFsRepayList(fsRepay);
        ExcelUtil<FsRepay> util = new ExcelUtil<FsRepay>(FsRepay.class);
        util.exportExcel(response, list, "还款明细数据");
    }

    /**
     * 获取还款明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:repay:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsRepayService.selectFsRepayById(id));
    }

    /**
     * 新增还款明细
     */
    @PreAuthorize("@ss.hasPermi('fs:repay:add')")
    @Log(title = "还款明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsRepay fsRepay) {
        int num = fsRepayService.insertFsRepay(fsRepay);
        resetBillSjhk(fsRepay.getBillId());
        return toAjax(num);
    }

    /**
     * 修改还款明细
     */
    @PreAuthorize("@ss.hasPermi('fs:repay:edit')")
    @Log(title = "还款明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsRepay fsRepay) {
        int num = fsRepayService.updateFsRepay(fsRepay);
        resetBillSjhk(fsRepay.getBillId());
        return toAjax(num);
    }

    /**
     * 删除还款明细
     */
    @PreAuthorize("@ss.hasPermi('fs:repay:remove')")
    @Log(title = "还款明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> billIds = fsRepayService.getBillIdsByIds(ids);
        int num = fsRepayService.deleteFsRepayByIds(ids);
        for (int i = 0; i < billIds.size(); i++) {
            resetBillSjhk(billIds.get(i));
        }
        return toAjax(num);
    }

    private void resetBillSjhk(Long billId) {
        FsRepay query = new FsRepay();
        query.setBillId(billId);
        List<FsRepay> repays = fsRepayService.selectFsRepayList(query);
        BigDecimal sjhk = new BigDecimal(0);
        if (!repays.isEmpty()) {
            for (int i = 0; i < repays.size(); i++) {
                sjhk = sjhk.add(repays.get(i).getHkje());
            }
        }
        FsBill bill = fsBillService.selectFsBillById(billId);
        bill.setShze(sjhk);
        fsBillService.updateFsBill(bill);
    }
}
